package com.zbw.common.controller;


import com.zbw.common.entity.*;
import com.zbw.common.mapper.BookMapper;
import com.zbw.common.service.BookContentService;
import com.zbw.common.service.FrontBookService;
import io.github.xxyopen.model.resp.RestResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("book")
@RequiredArgsConstructor
@Slf4j
public class FrontController {

    private final BookMapper bookMapper;

    private final FrontBookService frontBookService;

    private final Map<String, BookContentService> bookContentServiceMap;

    @RequestMapping("selectBook")
    public Book selecBook( Long Bid){

        Book book=frontBookService.selectBook(Bid);

        return book;
    }

//    @RequestMapping("queryComment")
//    public PageBean<BookCommentVO> queryComment
//            (@RequestBody AskBookComment ask){
//
//        PageBean<BookCommentVO> comment = frontBookService.selectComment(ask.getUserid(),ask.getBookId(),ask.getNum(),ask.getSize());
//
//        return comment;
//    }

    @RequestMapping("addVisitCount")
    public void addCount(@RequestBody Long bookId){

        frontBookService.addVisitCount(bookId,1);
    }
    @RequestMapping("listRecBooks")
    public List<Book> listRecBooksByCatId(@RequestBody Integer catId)
    {
        return frontBookService.listRecBooks(catId);
    }

    @RequestMapping("listRank")
    public List<Book> listRank(Byte type,Integer limit){
        return frontBookService.listRank(type,limit);
    }

    @RequestMapping("listBookCategory")
    public List<BookCategory> listBookCategory(){
        return frontBookService.listBookCategory();
    }

    @RequestMapping("queryIndexCount")
    public Long queryIndexCount(Long bookId){

        return frontBookService.queryIndexCount(bookId);
    }

    @RequestMapping("queryBookIndex")
    public BookIndex queryBookIndex(Long lastBookIndexId){

        return frontBookService.queryBookIndex(lastBookIndexId);
    }

    @RequestMapping("queryBookContent")
    public String queryBookContent(Long bookId, Long lastBookIndexId){
        BookIndex bookIndex = queryBookIndex(lastBookIndexId);
        String lastBookContent = bookContentServiceMap.get(bookIndex.getStorageType())
                .queryBookContent(bookId, lastBookIndexId).getContent();
        if (lastBookContent.length() > 42) {
            lastBookContent = lastBookContent.substring(0, 42);
        }
        return lastBookContent;
    }

    @RequestMapping("queryPreBookIndexId")
    public Long queryPreBookIndexId(Long bookId,  Integer indexNum){
        return frontBookService.queryPreBookIndexId(bookId,indexNum);
    }

    @RequestMapping("queryNextBookIndexId")
    public Long queryNextBookIndexId(Long bookId, Integer indexNum){
        return frontBookService.queryNextBookIndexId(bookId,indexNum);
    }

    @RequestMapping("queryBookContentBook")
    public BookContent queryBookContentBook(Long bookId,  Long bookIndexId){
        BookIndex bookIndex = queryBookIndex(bookIndexId);
        BookContent bookContent = bookContentServiceMap.get(bookIndex.getStorageType())
                .queryBookContent(bookId, bookIndexId);
        return bookContent;
    }

    @RequestMapping("queryIndexList")
    public List<BookIndex> queryIndexList
            (Long bookId,  String orderby,
              Integer page,  Integer pagesize){
        return frontBookService.queryIndexList(bookId,orderby,page,pagesize);
    }
    /**
     * 发布小说
     * @param book 小说信息
     * @param authorId 作家ID
     * @param penName 作家笔名
     * */
    @RequestMapping("addBook")
    RestResult<?> addBook(@RequestBody Book book,  Long authorId,  String penName){

        return frontBookService.addBook(book, authorId, penName );
    }

    @RequestMapping("updateBookStatus")
    void updateBookStatus( Long bookId,  Byte status,  Long authorId){

         frontBookService.updateBookStatus(bookId,status,authorId);
    }

    @RequestMapping("deleteIndex")
    void deleteIndex(Long indexId,  Long bookId){
        frontBookService.deleteIndex(indexId,bookId);
    }

    @RequestMapping("updateIndexName")
    void updateIndexName
            (Long indexId, String indexName,
             Long bookId){
        frontBookService.updateIndexName(indexId,indexName,bookId);
    }
    /**
     * 发布章节内容
     * @param bookId 小说ID
     * @param indexName 章节名
     * @param content 章节内容
     * @param isVip 是否收费
     * @param authorId 作者ID   */
    @RequestMapping("addBookContent")
    void addBookContent(Long bookId, String indexName,
                        String content, Byte isVip, Long authorId){
        frontBookService.addBookContent(bookId,indexName,content,isVip,authorId);
    }

    @RequestMapping("queryIndexContent")
    String queryIndexContent(Long indexId, Long authorId){

        return frontBookService.queryIndexContent(indexId,authorId);
    }

    @RequestMapping("updateBookContent")
    void updateBookContent(Long indexId, String indexName
            ,String content, Long authorId){
        frontBookService.updateBookContent(indexId,indexName,content,authorId);
    }

    @RequestMapping("updateBookPic")
    void updateBookPic(Long bookId,
                       String bookPic,
                        Long authorId){

        frontBookService.updateBookPic(bookId,bookPic,authorId);
    }
    @RequestMapping("addBookComment")
    void addBookComment(Long id, @RequestBody BookComment comment){
        frontBookService.addBookComment(id, comment);

    }
}
